<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>win32基础 | heit0ng</title><meta name="author" content="heit0ng"><meta name="copyright" content="heit0ng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="寒假学的，有点摸鱼学得很慢 一点点win32基础知识，感觉和学新语言差不多，还是得靠多练才行啊 字符编码正常ascii表(美国人发明)  基于ascii的扩展表GB2312或GB2312-80 专门表示中文(仅针对某个国家的编码)  两个十六进制组合成一个汉字，但有很大弊端，不同国家的编码不一样，同一种组合表示的意思并不相同 unicode编码一种编码方案，包含世界上所有的文字符号，范围:0-0x">
<meta property="og:type" content="article">
<meta property="og:title" content="win32基础">
<meta property="og:url" content="https://heit0ng.gitee.io/2022/02/11/WIN32/index.html">
<meta property="og:site_name" content="heit0ng">
<meta property="og:description" content="寒假学的，有点摸鱼学得很慢 一点点win32基础知识，感觉和学新语言差不多，还是得靠多练才行啊 字符编码正常ascii表(美国人发明)  基于ascii的扩展表GB2312或GB2312-80 专门表示中文(仅针对某个国家的编码)  两个十六进制组合成一个汉字，但有很大弊端，不同国家的编码不一样，同一种组合表示的意思并不相同 unicode编码一种编码方案，包含世界上所有的文字符号，范围:0-0x">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2022/02/11/zaOwVjM52ZRoAh3.jpg">
<meta property="article:published_time" content="2022-02-11T15:18:54.610Z">
<meta property="article:modified_time" content="2022-02-11T15:21:53.844Z">
<meta property="article:author" content="heit0ng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/02/11/zaOwVjM52ZRoAh3.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://heit0ng.gitee.io/2022/02/11/WIN32/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'win32基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-02-11 23:21:53'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2022/02/11/zaOwVjM52ZRoAh3.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">heit0ng</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">win32基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-11T15:18:54.610Z" title="发表于 2022-02-11 23:18:54">2022-02-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-11T15:21:53.844Z" title="更新于 2022-02-11 23:21:53">2022-02-11</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="win32基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>寒假学的，有点摸鱼学得很慢</p>
<p>一点点win32基础知识，感觉和学新语言差不多，还是得靠多练才行啊</p>
<h3 id="字符编码"><a href="#字符编码" class="headerlink" title="字符编码"></a>字符编码</h3><h4 id="正常ascii表"><a href="#正常ascii表" class="headerlink" title="正常ascii表"></a>正常ascii表</h4><p>(美国人发明)</p>
<p><img src="https://s2.loli.net/2022/02/11/bGdMxjFYsCVu3fn.jpg" alt="-1.jpg"></p>
<h4 id="基于ascii的扩展表"><a href="#基于ascii的扩展表" class="headerlink" title="基于ascii的扩展表"></a>基于ascii的扩展表</h4><p>GB2312或GB2312-80</p>
<p>专门表示中文(仅针对某个国家的编码)</p>
<p><img src="https://s2.loli.net/2022/02/11/VpmXve96flZkw5a.gif" alt="0.png"></p>
<p>两个十六进制组合成一个汉字，但有很大弊端，不同国家的编码不一样，同一种组合表示的意思并不相同</p>
<h4 id="unicode编码"><a href="#unicode编码" class="headerlink" title="unicode编码"></a>unicode编码</h4><p>一种编码方案，包含世界上所有的文字符号，范围:0-0x10FFFF</p>
<p>但仅赋予了编码，并没有规定二进制代码如何储存</p>
<h4 id="如何存储unicode"><a href="#如何存储unicode" class="headerlink" title="如何存储unicode"></a>如何存储unicode</h4><p>(一般存储默认为utf-16)</p>
<p>UTF-16/UTF-8是unicode的实现方式</p>
<p>utf-16:以16位无符号整数为单位，16位仅仅表示一个单位，不表示一个字符只有16位</p>
<p>位数补齐，一位补成两位，三位补成四位，但网络传输十分麻烦</p>
<p>utf-8：是两位就存两位，三位就存三位，更方便，但解析麻烦</p>
<p><img src="https://s2.loli.net/2022/02/11/9TyHFmcRJ5SW7Ik.png" alt="1.png"></p>
<h4 id="BOM（byte-order-mark）"><a href="#BOM（byte-order-mark）" class="headerlink" title="BOM（byte order mark）"></a>BOM（byte order mark）</h4><p>文件起始编码表示存储方式</p>
<p>UTF-8            EF BB BF</p>
<p>UTF-16LE      FF FE 小端(倒过来)</p>
<p>UTF-16BE      FE FF 大端</p>
<h3 id="win32的变量类型"><a href="#win32的变量类型" class="headerlink" title="win32的变量类型"></a>win32的变量类型</h3><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/happycat1988/archive/2013/04/14/3020820.html">https://www.cnblogs.com/happycat1988/archive/2013/04/14/3020820.html</a></p>
<h3 id="win32宽字符"><a href="#win32宽字符" class="headerlink" title="win32宽字符"></a>win32宽字符</h3><p>主要与MessageBox()这个API函数相关联</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">    /*    int MessageBox(</span><br><span class="line">        HWND hWND,   //handle to owner window   句柄 </span><br><span class="line">        LPCSTR IpText,  //text in message box  内容 </span><br><span class="line">        LPCSTR IpCption, //message box title  窗口的标题 </span><br><span class="line">        UINT uType       //message box style  窗口的格式 </span><br><span class="line">        )</span><br><span class="line">      */</span><br><span class="line"></span><br><span class="line">//    CHAR message[]=&quot;hellow world&quot;;</span><br><span class="line">//    CHAR title[]=&quot;messagebox&quot;;</span><br><span class="line">//    MessageBoxA(NULL,message,title,MB_OK);   /*ASCALL   单位为一个字节*/ </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//    WCHAR message[]=L&quot;hellow world&quot;;</span><br><span class="line">//    WCHAR title[]=L&quot;messagebox&quot;;</span><br><span class="line">//    MessageBoxW(NULL,message,title,MB_OK);   /*unicode  一个单位为两个字节*/</span><br><span class="line"></span><br><span class="line">//    TCHAR message[]=TEXT(&quot;hellow world&quot;);</span><br><span class="line">//    TCHAR title[]=TEXT(&quot;messagebox&quot;);</span><br><span class="line">//    MessageBox(NULL,message,title,MB_OK);    /*可以根据项目的要求来决定是什么编码方式*/</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="进程的创建过程"><a href="#进程的创建过程" class="headerlink" title="进程的创建过程"></a>进程的创建过程</h3><h5 id="1-什么是进程"><a href="#1-什么是进程" class="headerlink" title="1.什么是进程"></a>1.什么是进程</h5><p>进程提供所需要的资源，如：数据，代码等等（资源如何使用与他无关）</p>
<p>线程负责使用资源，一般都是多线程</p>
<h5 id="2-进程内存空间的地址划分"><a href="#2-进程内存空间的地址划分" class="headerlink" title="2.进程内存空间的地址划分"></a>2.进程内存空间的地址划分</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">分区                 X86 32位windows</span><br><span class="line">空指针赋值区          0x00000000-0x0000FFFF//从来没有被使用或占用(一般)</span><br><span class="line">用户模式区            0x00010000-0x7FFEFFFF//我们使用的，加载exe,dll的地方</span><br><span class="line">64KB禁入区			  0x7FFF0000-0X7FFFFFFF//操作系统和我们通常也不用</span><br><span class="line">内核                 0x80000000-0xFFFFFFFF//内核占用，一般不变</span><br></pre></td></tr></table></figure>

<h5 id="3-进程的创建"><a href="#3-进程的创建" class="headerlink" title="3.进程的创建"></a>3.进程的创建</h5><p>&lt;1&gt;任何进程都是别的进程创建的:</p>
<p>第一个进程是操作系统创建的</p>
<p>电脑上的进程由explorer调用CreateProcess()创建</p>
<p>&lt;2&gt;进程的创建过程</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1.映射EXE文件             //相当于把用的exe放入内存</span><br><span class="line">2.创建内核对象EPROCESS</span><br><span class="line">3.映射系统Dll(ntdll.dll)  //映射的第一个dll</span><br><span class="line">4.创建线程内核对象ETHREAD</span><br><span class="line">5.系统启动线程</span><br><span class="line">	(如果对这些过程熟悉可以进行注入,例如:在此步将A换成B的dll，执行B程序，但是如任务管理器看到的仍然是A)</span><br><span class="line">    映射DLL(ntdll.LdrlnitializeThunk)  //加载其他的dll，有些dll会引用其他的，所以看着的dll会很多</span><br><span class="line">    最后线程开始执行</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/02/11/TZIdMm9tlz5DvY8.png" alt="2.png"></p>
<h3 id="创建进程"><a href="#创建进程" class="headerlink" title="创建进程"></a>创建进程</h3><p>关于CreateProcess</p>
<p>(具体可以进行查找)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">BOOL CreateProcess</span><br><span class="line">(</span><br><span class="line">    LPCTSTR lpApplicationName,  //指向一个NULL结尾的、用来指定可执行模块的字符串。</span><br><span class="line">    LPTSTR lpCommandLine,  //指向一个以NULL结尾的字符串，该字符串指定要执行的命令行。</span><br><span class="line">    LPSECURITY_ATTRIBUTES lpProcessAttributes,</span><br><span class="line">    LPSECURITY_ATTRIBUTES lpThreadAttributes,</span><br><span class="line">    BOOL bInheritHandles,</span><br><span class="line">    DWORD dwCreationFlags,</span><br><span class="line">    LPVOID lpEnvironment,</span><br><span class="line">    LPCTSTR lpCurrentDirectory,</span><br><span class="line">    LPSTARTUPINFO lpStartupInfo,  //STARTUPINFO，指定一个STARTUPINFO结构，其中包含了创建进程时使用的附加信息</span><br><span class="line">    LPPROCESS_INFORMATIONlpProcessInformation   //PROCESS_INFORMATION，该结构用于容纳新进程的进程和线程标识符。大多数情况下，一旦这个函数返回，父应用程序都会关闭两个句柄。</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>  例如</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line">BOOL CreateChildProcess(PTCHAR szChildname,PTCHAR szCommandLine)</span><br><span class="line">&#123;</span><br><span class="line">    STARTUPINFO si;//必须使用，创建一个结构体</span><br><span class="line">    PROCESS_INFORMATION pi;</span><br><span class="line">    </span><br><span class="line">    ZeroMemory(&amp;pi,sizeof(pi));//用来将指定的内存块清零(将结构体清零)</span><br><span class="line">    ZeroMemory(&amp;si,sizeof(si));</span><br><span class="line">    si.cb = sizeof(si);</span><br><span class="line"></span><br><span class="line">    //创建子进程 返回成功与失败</span><br><span class="line">    if(!CreateProcess(</span><br><span class="line">        szChildname,//对象名称</span><br><span class="line">        szCommandLine,//命令行</span><br><span class="line">        NULL,  //不继承进程句柄</span><br><span class="line">        NULL,  //不继承线程句柄</span><br><span class="line">        FALSE, //不继承句柄</span><br><span class="line">        0,     //没有创建标志，常用的有一个CREATE_SUSPENDED，立即挂起新进程。除非调用了ResumeThread函数函数，否则它不会恢复运行。可以用来干坏事。</span><br><span class="line">        NULL,//使用父进程环境变量</span><br><span class="line">        NULL,//使用父进程目录作为当前目录，可以自己设置目录</span><br><span class="line">        &amp;si,//STARTUPINFOW结构体详细信息,存储的是创建子进程的各种详细信息(必须使用)</span><br><span class="line">        &amp;pi)//PROCESS_INFORMATION结构体进程信息，相当于传返回值</span><br><span class="line">    )</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;CreateChildProcess Error%d&quot;,GetLastError());   //用来返回大概错误信息，可在tool用返回的数字查找错误原因</span><br><span class="line">        return FALSE;</span><br><span class="line">    &#125;</span><br><span class="line">    CloseHandle(pi.hProcess);   //关闭句柄</span><br><span class="line">    CloseHandle(pi.hThread);</span><br><span class="line">    return TRUE;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(int argc,char*argv[])</span><br><span class="line">&#123;</span><br><span class="line">    TCHAR lpApplicationName[]=TEXT(&quot;C:/Program Files/Mozilla Firefox/firefox.exe&quot;);//对象名称</span><br><span class="line">    TCHAR lpCommandLine[]=TEXT(&quot; https://heit0ng.gitee.io/&quot;);//命令行(参数)</span><br><span class="line"></span><br><span class="line">    CreateChildProcess(lpApplicationName,lpCommandLine);</span><br><span class="line"></span><br><span class="line">    getchar();</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>相关的2个结构体</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">typedef struct _PROCESS_INFORMATION &#123;</span><br><span class="line">    HANDLE hProcess;  //进程句柄</span><br><span class="line">    HANDLE hThread;   //线程句柄</span><br><span class="line">    DWORD dwProcessId;//进程id</span><br><span class="line">    DWORD dwThreadId; //线程id</span><br><span class="line">  &#125; PROCESS_INFORMATION, *PPROCESS_INFORMATION, *LPPROCESS_INFORMATION;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">typedef struct _STARTUPINFOA &#123;</span><br><span class="line">    DWORD cb;   //第一个是用来储存这个结构体的size的。</span><br><span class="line">    LPSTR lpReserved;</span><br><span class="line">    LPSTR lpDesktop;</span><br><span class="line">    LPSTR lpTitle;</span><br><span class="line">    DWORD dwX;</span><br><span class="line">    DWORD dwY;</span><br><span class="line">    DWORD dwXSize;</span><br><span class="line">    DWORD dwYSize;</span><br><span class="line">    DWORD dwXCountChars;</span><br><span class="line">    DWORD dwYCountChars;</span><br><span class="line">    DWORD dwFillAttribute;</span><br><span class="line">    DWORD dwFlags;</span><br><span class="line">    WORD wShowWindow;</span><br><span class="line">    WORD cbReserved2;</span><br><span class="line">    LPBYTE lpReserved2;</span><br><span class="line">    HANDLE hStdInput;</span><br><span class="line">    HANDLE hStdOutput;</span><br><span class="line">    HANDLE hStdError;</span><br><span class="line">  &#125; STARTUPINFOA, *LPSTARTUPINFOA;</span><br></pre></td></tr></table></figure>

<p>关于<strong>ZeroMemory()</strong></p>
<p>ZeroMemory只是将指定的内存块清零。<br>使用结构前清零，而不让结构的成员数值具有不确定性。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">void ZeroMemory( PVOID Destination,SIZE_T Length );</span><br><span class="line"></span><br><span class="line">Destination :指向一块准备用0来填充的内存区域的开始地址。</span><br><span class="line">Length :准备用0来填充的内存区域的大小，按字节来计算。</span><br></pre></td></tr></table></figure>

<h3 id="句柄表"><a href="#句柄表" class="headerlink" title="句柄表"></a>句柄表</h3><p>句柄，当前进程私有的一张表的索引</p>
<p>1.什么是内核对象</p>
<p>像进程，线程，文件，互斥体，事件等在内核都有一个对应的结构体，这些结构体由内核负责管理，我们管这样的对象叫做内核对象。</p>
<p><img src="https://i.loli.net/2021/03/04/N91iX5qbzGMSKjv.png" alt="Snipaste_2021-03-04_17-13-13.png"></p>
<p>2.如何管理内核对象(紫色的为相对应的地址)</p>
<p><img src="https://s2.loli.net/2022/02/11/bsSLIaRngEXvcF4.png" alt="4.png"></p>
<p>(蓝色的为句柄表，里面的数字为句柄，相当于索引，想操作内核对象只能通过索引，句柄指向对应的内核对象)</p>
<p>操作系统有一个特殊的句柄，叫做全局句柄表。</p>
<p><img src="https://i.loli.net/2021/03/04/NcJTGi67zhjDB9r.png" alt="Snipaste_2021-03-04_17-29-03.png"></p>
<p>4.多进程可以共享一个内核对象</p>
<p>5.句柄是否“可以“被继承</p>
<p>上述蓝色表中因还有一列判断是否可以被继承</p>
<h3 id="进程相关API"><a href="#进程相关API" class="headerlink" title="进程相关API"></a>进程相关API</h3><p><strong>id(又称pid)</strong></p>
<p>每一个进程的编号不会改变</p>
<p><img src="https://i.loli.net/2021/03/04/SQqh2uGOo3j7mTx.png" alt="Snipaste_2021-03-04_17-35-04.png"></p>
<p><strong>SuspendThread()</strong></p>
<p>SuspendThread，暂停指定的线程。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DWORD WINAPI SuspendThread(</span><br><span class="line">　　_In_HANDLE hThread</span><br><span class="line">　　);</span><br></pre></td></tr></table></figure>

<p><strong>TerminateProcess()</strong></p>
<p>终止指定的进程和其线程</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">BOOL TerminateProcess(</span><br><span class="line">    HANDLE hProcess,  //进程句柄</span><br><span class="line">    UINT uExitCode    //进程终止码，进程因为什么原因退出</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>OpenProcess()</strong></p>
<p>penProcess 函数用来打开一个已存在的进程对象，并返回进程的句柄。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">HANDLE OpenProcess(</span><br><span class="line">    DWORD dwDesiredAccess, //渴望得到的访问权限（标志），权限很多</span><br><span class="line">    BOOL bInheritHandle, // 是否继承句柄</span><br><span class="line">    DWORD dwProcessId// 进程标示符</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>ResumeThread()</strong></p>
<p>ResumeThread，启动指定的线程。(可以在这步之前进行注入)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DWORD WINAPI ResumeThread(</span><br><span class="line">_In_ HANDLE hThread</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>GetModuleFileName()</strong></p>
<p>得到模块路径，如exe在哪模块路径就在哪</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">DWORDGetModuleFileName(</span><br><span class="line">HMODULE  hModule,//装载一个程序实例的句柄。如果该参数为NULL，该函数返回该当前应用程序全路径。</span><br><span class="line">LPTSTR  lpFilename,</span><br><span class="line">DWORD  nSize</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>GetCurrentDirectory()</strong></p>
<p>得到工作路径，创建这个进程的人添加所得</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">DWORD GetCurrentDirectory(</span><br><span class="line">DWORD nBufferLength,     //sizeofdirectorybuffer  缓冲区的长度</span><br><span class="line">LPTSTR lpBuffer          //directorybuffer  指定一个预定义字串，用于装载当前目录</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>如在Vc6中：<img src="https://s2.loli.net/2022/02/11/BEeazRbIniWADf7.png" alt="5.png"></p>
<p><strong>以挂起的形式创建流程</strong></p>
<p>在<strong>进程的创建</strong>过程中第五步程序会等着操作，然后恢复以后再继续执行</p>
<p><strong>其他进程相关API</strong></p>
<p>获取进程PID      GetCurrentProcessld</p>
<p>获取进程句柄    GetCurrentProcess</p>
<p>获取命令行        GetCommandLine</p>
<p>获取启动信息    GetStartupInfo</p>
<p>遍历进程ID        EnumProcesses</p>
<p>CreateToolhelp32Snapshot()</p>
<p>可以通过获取进程信息为指定的进程、进程使用的堆[HEAP]、模块[MODULE]、线程建立一个快照。</p>
<h3 id="创建线程"><a href="#创建线程" class="headerlink" title="创建线程"></a>创建线程</h3><p><strong>1.什么是线程？</strong></p>
<p>(1)线程是附属在进程上的执行实体，是代码的执行流程。</p>
<p>(2)一个进程可以包含多个线程，但一个进程至少要包含一个线程。</p>
<p><strong>2.创建线程</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">HANDLE CreateThread(</span><br><span class="line">    LPSECURITY_ATTRIBUTES lpThreadAttributes,//SD</span><br><span class="line">    SIZE_T dwStackSize,//initialstacksize   初始堆栈大小，未指定的话为系统默认值</span><br><span class="line">    LPTHREAD_START_ROUTINE lpStartAddress,//线程执行的代码位置</span><br><span class="line">    LPVOID lpParameter,//threadargument   线程参数</span><br><span class="line">    DWORD dwCreationFlags,//creationoption   创建选项</span><br><span class="line">    LPDWORD lpThreadId//threadidentifier   线程标识符</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>CreateThread(NULL,0,0,NULL,0,NULL);</p>
<p>第一个参数:表示线程内核对象的安全属性，一般传入NULL表示使用默认设置。</p>
<p>第二个:0表示操作系统默认值一般为1MB</p>
<p>第三个:新线程所执行的线程函数地址，多个线程可以使用同一个函数地址。</p>
<p>第四个:参数</p>
<p>第五个:指定额外的标志来控制线程的创建，为0表示可立即使用</p>
<p>第六个:线程id范围值，NULL表示不接收</p>
<p><strong>ThreadProc()</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DWORD WINAPI ThreadProc(</span><br><span class="line">LPVOID lpParameter</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>lpParameter<br>接收线程传递给函数使用的CreateThread函数lpParameter参数数据。</p>
<p>返回值:<br>函数应该返回一个值，表示线程函数返回退出码，一般使用0作为返回值。<br>线程创建成功,返回非零值,否则为0。</p>
<h3 id="线程控制"><a href="#线程控制" class="headerlink" title="线程控制"></a>线程控制</h3><p>与线程相关的API函数</p>
<h4 id="1-让线程停下来"><a href="#1-让线程停下来" class="headerlink" title="1.让线程停下来"></a><strong>1.让线程停下来</strong></h4><p>​    自己停下来 sleep()函数，参数1000表示1秒</p>
<p>​    让别人停下来，暂停指定的线程 SuspendThread()函数</p>
<p>​    恢复线程 ResumeThread()</p>
<h4 id="2-等待线程结束"><a href="#2-等待线程结束" class="headerlink" title="2.等待线程结束"></a><strong>2.等待线程结束</strong></h4><p>1.WaitForSingleObject()</p>
<p>只能等待某一个内核对象的状态变更</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">DWORD WINAPI WaitForSingleObject(</span><br><span class="line">    __in HANDLE hHandle,//句柄，等待的对象</span><br><span class="line">    __in DWORD dwMilliseconds//等待时间，可以指定时间，如果指定时间内状态未变更，仍然返回</span><br><span class="line">    如果一直等待，设置成INFINITE</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>2.WaitForMultipleObjects()</p>
<p>可等待多个内核对象</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DWORD WaitForMultipleObjects(</span><br><span class="line">    DWORD nCount,    //等待几个内核对象，句柄数</span><br><span class="line">    const HANDLE* lpHandles,   //等待内核对象的数组</span><br><span class="line">    BOOL bWaitAll,   //等待模式，如等所有对象发生变更或某一内核对象发生变更才返回两种模式TRUE和</span><br><span class="line">    DWORD dwMilliseconds  //等待时间</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>3.GetExitCodeThread()</p>
<p>获取结束线程的代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">BOOL GetExitCodeThread(</span><br><span class="line">     HANDLE hThread,  //获取退出代码的一个线程的句柄</span><br><span class="line">     LPDWORD lpExitCode   //存储线程结束代码,也就是线程的返回值</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h4 id="3-设置，获取线程上下文"><a href="#3-设置，获取线程上下文" class="headerlink" title="3.设置，获取线程上下文"></a>3.设置，获取线程上下文</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">BOOL GetThreadContext(</span><br><span class="line">	HANDLE hThread,</span><br><span class="line">	LPCONTEXT ipContext</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">BOOL SetThreadContext(</span><br><span class="line">	HADNLE hThread,</span><br><span class="line">	CONST CONTEXT *IpContext</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h3 id="临界区"><a href="#临界区" class="headerlink" title="临界区"></a>临界区</h3><p><strong>1.线程安全问题</strong></p>
<p>每个线程都有自己的栈，而局部变量是存储在栈中的，这就意味着每个线程都有一份自己的”局部变量”，如果线程仅仅使用”局部变量”那么就不存在线程安全问题，当多个线程同时调用一个全局变量时候会有线程安全问题</p>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line"></span><br><span class="line">int g_dwTickets = 10;</span><br><span class="line">DWORD WINAPI ThreadProc(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">    while(g_dwTickets&gt;0)//判断余票 </span><br><span class="line">    &#123;</span><br><span class="line">		printf(&quot;还有%d张票\n&quot;,g_dwTickets);</span><br><span class="line">		g_dwTickets--;</span><br><span class="line">		printf(&quot;卖出一张，还有%d张\n&quot;,g_dwTickets); </span><br><span class="line">	&#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line">int main(int argc,char* argv[])</span><br><span class="line">&#123;</span><br><span class="line">    DWORD dwResult1;</span><br><span class="line">    DWORD dwResult2;</span><br><span class="line">    HANDLE aThreadHandles[2];</span><br><span class="line">    aThreadHandles[0] = CreateThread(</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	ThreadProc,</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	NULL</span><br><span class="line">	);</span><br><span class="line">	</span><br><span class="line">	aThreadHandles[1] = CreateThread(</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	ThreadProc,</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	NULL</span><br><span class="line">	);//两个线程相当于两个窗口卖票 </span><br><span class="line">	WaitForMultipleObjects(2,aThreadHandles,TRUE,INFINITE);</span><br><span class="line">	GetExitCodeThread(aThreadHandles[0],&amp;dwResult1);</span><br><span class="line">	GetExitCodeThread(aThreadHandles[1],&amp;dwResult2);</span><br><span class="line">	printf(&quot;%d %d \n&quot;,dwResult1,dwResult2);</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2022/02/11/DBFWNX1MzsQiCwS.png" alt="6.png"></p>
<p>显然存在问题，最后出现了-1</p>
<p>为什么呢?</p>
<p>两段代码都在访问g_dwTickets一个全局变量</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">线程1：</span><br><span class="line">DWORD WINAPI ThreadProc(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">    while(g_dwTickets&gt;0)//判断余票 </span><br><span class="line">    &#123;//A</span><br><span class="line">		printf(&quot;还有%d张票\n&quot;,g_dwTickets);</span><br><span class="line">		g_dwTickets--;</span><br><span class="line">		printf(&quot;卖出一张，还有%d张\n&quot;,g_dwTickets); </span><br><span class="line">	&#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">线程2:</span><br><span class="line">DWORD WINAPI ThreadProc(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">    while(g_dwTickets&gt;0)//判断余票 </span><br><span class="line">    &#123;</span><br><span class="line">		printf(&quot;还有%d张票\n&quot;,g_dwTickets);</span><br><span class="line">		g_dwTickets--;</span><br><span class="line">		printf(&quot;卖出一张，还有%d张\n&quot;,g_dwTickets); </span><br><span class="line">	&#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line">假设还有一张票，当走到A时线程被切换到线程2接着跑，此时判断仍有余票，此线程执行完变成0，然后从A接着走此时变为-1</span><br></pre></td></tr></table></figure>

<p><strong>2.如何解决</strong></p>
<p>临界资源:一次只允许一个线程进行的全局变量</p>
<p>临界区:访问临界资源的那段代码</p>
<p>可用WINDOWS的API实现临界区，自己也可以写一个</p>
<p><img src="https://s2.loli.net/2022/02/11/wn2qJB9KGzXkMWV.png" alt="7.png"></p>
<p>用令牌作为一个标志，判断全局变量是否被使用</p>
<p><strong>3.临界区实现之线程锁</strong></p>
<p>1.创建全局变量</p>
<p>​    CRITICAL_SECTION cs;//相当于令牌</p>
<p>2.初始化全局变量</p>
<p>​    InitializeCriticalSection(&amp;cs);</p>
<p>3.实现临界区</p>
<p>​    EnterCriticalSection(&amp;cs);</p>
<p>​    //使用临界资源</p>
<p>​    LeaveCriticalSection(&amp;cs);</p>
<p>修改后</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line"></span><br><span class="line">int g_dwTickets = 10;</span><br><span class="line">//全局变量</span><br><span class="line">CRITICAL_SECTION cs;</span><br><span class="line">DWORD WINAPI ThreadProc(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">	//实现临界区</span><br><span class="line">	EnterCriticalSection(&amp;cs);</span><br><span class="line">    while(g_dwTickets&gt;0)//判断余票 </span><br><span class="line">    &#123;	</span><br><span class="line">		printf(&quot;还有%d张票\n&quot;,g_dwTickets);</span><br><span class="line">		g_dwTickets--;</span><br><span class="line">		printf(&quot;卖出一张，还有%d张\n&quot;,g_dwTickets); </span><br><span class="line">	&#125;</span><br><span class="line">	   LeaveCriticalSection(&amp;cs);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(int argc,char* argv[])</span><br><span class="line">&#123;</span><br><span class="line">    DWORD dwResult1;</span><br><span class="line">    DWORD dwResult2;</span><br><span class="line">    HANDLE aThreadHandles[2];</span><br><span class="line">    </span><br><span class="line">    InitializeCriticalSection(&amp;cs);</span><br><span class="line">    	</span><br><span class="line">    aThreadHandles[0] = CreateThread(</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	ThreadProc,</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	NULL</span><br><span class="line">	);</span><br><span class="line">	</span><br><span class="line">	aThreadHandles[1] = CreateThread(</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	ThreadProc,</span><br><span class="line">	NULL,</span><br><span class="line">	0,</span><br><span class="line">	NULL</span><br><span class="line">	);//两个线程相当于两个窗口卖票 </span><br><span class="line">	WaitForMultipleObjects(2,aThreadHandles,TRUE,INFINITE);</span><br><span class="line">	GetExitCodeThread(aThreadHandles[0],&amp;dwResult1);</span><br><span class="line">	GetExitCodeThread(aThreadHandles[1],&amp;dwResult2);</span><br><span class="line">	printf(&quot;%d %d \n&quot;,dwResult1,dwResult2);</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="互斥体"><a href="#互斥体" class="headerlink" title="互斥体"></a>互斥体</h3><p>相当于一个令牌放在内核里</p>
<p><img src="https://s2.loli.net/2022/02/11/RuqyYThGn3g4Fis.png" alt="8.png"></p>
<p>CreateMutex是一个计算机函数，作用是找出当前系统是否已经存在指定进程的实例。如果没有则创建一个互斥体。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">HANDLE CreateMutex(</span><br><span class="line">    LPSECURITY_ATTRIBUTES lpMutexAttributes, // 指向安全属性的指针，一般设置为NULL</span><br><span class="line">    BOOL bInitialOwner, // 初始化互斥对象的所有者，如创建进程希望立即拥有互斥体，则设为TRUE。一个互斥体同时只能由一个线程拥有。</span><br><span class="line">    LPCTSTR lpName // 指向互斥对象名的指针  </span><br><span class="line">);</span><br><span class="line">BOOL bInitialOwner TRUE代表就是当前线程的，FALSE代表这个互斥体不是当前线程只是将他创建出来</span><br></pre></td></tr></table></figure>

<p><strong>互斥体与线程锁的区别</strong>(比如防止游戏多开)</p>
<p>1.线程锁只能用于单个进程间的线程控制</p>
<p>2.互斥体可以设定等待超时，但线程锁不能</p>
<p>3.线程意外终结时，Mutex可以避免无限等待//比如拿到令牌还没来得及释放进程结束了</p>
<p>4.Mutex的效率没有线程锁高</p>
<p>进程1</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">    int i;</span><br><span class="line">    //创建一个互斥体 </span><br><span class="line">    HANDLE g_hMutex=CreateMutex(NULL,TRUE,&quot;name&quot;);</span><br><span class="line">    //获取令牌：有信号 或者线程的拥有者(TRUE)</span><br><span class="line">    WaitForSingleObject(g_hMutex,INFINITE);</span><br><span class="line">    for(i=0;i&lt;10;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        Sleep(1000);</span><br><span class="line">        printf(&quot;hellow world\n&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    //释放令牌 </span><br><span class="line">    ReleaseMutex(g_hMutex);</span><br><span class="line">    getchar(); </span><br><span class="line">    return 0;</span><br><span class="line"> &#125; </span><br></pre></td></tr></table></figure>

<p>进程2</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">    int i;</span><br><span class="line">    //创建一个互斥体 </span><br><span class="line">    HANDLE g_hMutex=CreateMutex(NULL,TRUE,&quot;name&quot;);</span><br><span class="line">    //获取令牌：有信号 或者线程的拥有者(TRUE)</span><br><span class="line">    WaitForSingleObject(g_hMutex,INFINITE);</span><br><span class="line">    for(i=0;i&lt;10;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        Sleep(1000);</span><br><span class="line">        printf(&quot;hellow world\n&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    //释放令牌 </span><br><span class="line">    ReleaseMutex(g_hMutex);</span><br><span class="line">    getchar(); </span><br><span class="line">    return 0;</span><br><span class="line"> &#125; </span><br></pre></td></tr></table></figure>

<p>当一个线程结束后，另一个线程才能执行</p>
<h3 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h3><p>和互斥不同，发出指令，线程都可以运行</p>
<p><strong>CreateEvent()</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">HANDLE CreateEvent(</span><br><span class="line">    LPSECURITY_ATTRIBUTES lpEventAttributes,// 安全属性</span><br><span class="line">    BOOL bManualReset,// 复位方式，TRUE为通知类型，FALSE为互斥类型。</span><br><span class="line">    BOOL bInitialState,// 初始状态，FALSE为无信号，TURE为有信号</span><br><span class="line">    LPCTSTR lpName // 对象名称</span><br><span class="line">);</span><br><span class="line">bManualReset：指定将事件对象创建成手动复原还是自动复原。如果是TRUE，那么必须用ResetEvent函数来手工将事件的状态复原到无信号状态。如果设置为FALSE，当一个线程等待到事件信号后系统会自动将事件状态复原为无信号状态。</span><br></pre></td></tr></table></figure>

<p>有信号:创建出来立马可以使用wait for single object这种函数或者其他去得到他。</p>
<p>没信号:就处于等待状态</p>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line">#include&lt;windows.h&gt;</span><br><span class="line">HANDLE g_hEvent;</span><br><span class="line"></span><br><span class="line">DWORD WINAPI ThreadProc_1(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">	TCHAR szBuffer[10]=&#123;0&#125;;</span><br><span class="line">	//当事件变成已通知时</span><br><span class="line">	WaitForSingleObject(g_hEvent,INFINITE);	</span><br><span class="line">	//线程执行</span><br><span class="line">	printf(&quot;ThreadProc_1执行了\n&quot;);</span><br><span class="line">	</span><br><span class="line">	getchar();</span><br><span class="line">	return 0; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">DWORD WINAPI ThreadProc_2(LPVOID lpParameter)</span><br><span class="line">&#123;</span><br><span class="line">	TCHAR szBuffer[10]=&#123;0&#125;;</span><br><span class="line">	//当事件变成已通知时</span><br><span class="line">	WaitForSingleObject(g_hEvent,INFINITE);	</span><br><span class="line">	//线程执行</span><br><span class="line">	printf(&quot;ThreadProc_2执行了\n&quot;);</span><br><span class="line">	</span><br><span class="line">	getchar();</span><br><span class="line">	return 0; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">int main(int argc,char* argv[])</span><br><span class="line">&#123;</span><br><span class="line">	//创建事件 </span><br><span class="line">	//默认安全属性    TRUE通知/FALSE互斥  初始无信号  初始无名字</span><br><span class="line">	g_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); </span><br><span class="line">	HANDLE hThread[2];</span><br><span class="line">	//创建两个线程</span><br><span class="line">	hThread[0]= CreateThread(NULL,0,ThreadProc_1,NULL,0,NULL);</span><br><span class="line">	hThread[1]= CreateThread(NULL,0,ThreadProc_2,NULL,0,NULL);</span><br><span class="line">	//设置事件为已通知</span><br><span class="line">	SetEvent(g_hEvent); </span><br><span class="line">	//等待线程结束，销毁内核对象</span><br><span class="line">	WaitForMultipleObjects(2,hThread,TRUE,INFINITE);</span><br><span class="line">	CloseHandle(hThread[0]);</span><br><span class="line">	CloseHandle(hThread[1]); </span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>线程同步</strong></p>
<p>1.线程互斥:线程互斥是指对于共享的进程系统资源，在各个单个线程访问时的排他性。当有若干个线程都要使用某一共享资源时，任何时刻最多只允许一个线程去使用，其他要使用该资源的线程必须等待，直到占用资源者释放该资源。</p>
<p>2.线程同步:线程同步是指线程之间的一种制约关系，一个线程的执行依赖另一个线程的消息，当他没有得到另一个线程消息时处于等待，直到消息传达才被唤醒。</p>
<p>同步的前提是互斥，互斥没有排序概念，可能一个线程执行多次，但同步是有序的</p>
<p>互斥+有序=同步</p>
<h3 id="窗口的本质"><a href="#窗口的本质" class="headerlink" title="窗口的本质"></a>窗口的本质</h3><p>   <img src="https://s2.loli.net/2022/02/11/UljNnpeu3L9h67A.png" alt="9.png"></p>
<p>用WINDOWS现有的界面为GUI，自己画的为GDI(简单了解)</p>
<p><strong>GID图形设备接口</strong></p>
<p>1.设备对象</p>
<p>2.DC(设备上下文)</p>
<p>3.图形对象</p>
<p><img src="https://s2.loli.net/2022/02/11/4qV3nI7FgwJrA5m.png" alt="10.png"></p>
<h3 id="第一个Windows程序"><a href="#第一个Windows程序" class="headerlink" title="第一个Windows程序"></a>第一个Windows程序</h3><p><strong>WinMain()</strong></p>
<p>WinMain是一个函数，该函数的功能是被系统调用，作为一个32位应用程序的入口点。WinMain函数应初始化应用程序，显示主窗口，进入一个消息接收一发送循环，这个循环是应用程序执行的其余部分的顶级控制结构。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int APIENTRY WinMain(</span><br><span class="line">    HINSTANCE hInstance,  //模块句柄，应用程序当前实例的句柄。</span><br><span class="line">    HINSTANCE hPrevInstance,  //Null</span><br><span class="line">    LPSTR     lpCmdLine,  //指向应用程序命令行的字符串的指针</span><br><span class="line">    int       nCmdShow) //指明窗口如何显示，例如最大化，最小化，具体请查阅。</span><br></pre></td></tr></table></figure>

<p><strong>sprintf()</strong></p>
<p>sprintf指的是字符串格式化命令，函数声明为 int sprintf(char *string, char *format [,argument,…]);，主要功能是把格式化的数据写入某个字符串中，即发送格式化输出到 string 所指向的字符串。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int sprintf(char *string, char *format [,argument,...]);</span><br></pre></td></tr></table></figure>

<p><strong>WNDCLASS</strong></p>
<p>WNDCLASS是一个由系统支持的结构，用来储存某一类窗口的信息,如ClassStyle,消息处理函数，Icon,Cursor,背景Brush等。也就是说，CreateWindow只是将某个WNDCLASS定义的窗体变成实例。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">typedef struct _WNDCLASS &#123;</span><br><span class="line">    UINT style;// 窗口类的风格</span><br><span class="line">    WNDPROC lpfnWndProc;//窗口处理函数</span><br><span class="line">    int cbClsExtra;//窗口扩展</span><br><span class="line">    int cbWndExtra;//窗口实例扩展</span><br><span class="line">    HINSTANCE hInstance;//实例句柄</span><br><span class="line">    HICON hIcon;//窗口的最小化图标</span><br><span class="line">    HCURSOR hCursor;//窗口鼠标光标</span><br><span class="line">    HBRUSH hbrBackground;//窗口背景色</span><br><span class="line">    LPCTSTR lpszMenuName;//窗口菜单</span><br><span class="line">    LPCTSTR lpszClassName;// 窗口类名</span><br><span class="line">&#125; WNDCLASS, *LPWNDCLASS;</span><br></pre></td></tr></table></figure>

<p><strong>WndProc()</strong></p>
<p>Wndproc是Windows操作系统向应用程序发送一系列消息之一，每个窗口会有一个窗口过程的回调函数，分别是窗口句柄、消息ID、WPARAM、LPARAM。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">LRESULT CALLBACK WndProc( //WndProc名称可自由定义</span><br><span class="line">    HWND hwnd,//窗口句柄</span><br><span class="line">    UINT uMsg,//消息ID</span><br><span class="line">    WPARAM wParam,//消息参数</span><br><span class="line">    LPARAM lParam//消息参数</span><br><span class="line">)；</span><br></pre></td></tr></table></figure>

<p><strong>CreateWindow()</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">HWND WINAPI CreateWindow(</span><br><span class="line"></span><br><span class="line">  _In_opt_  LPCTSTR lpClassName,  // 窗口类名称    </span><br><span class="line">  _In_opt_  LPCTSTR lpWindowName, // 窗口标题    </span><br><span class="line">  _In_      DWORD dwStyle,        // 窗口风格，或称窗口格式    </span><br><span class="line">  _In_      int x,                // 初始 x 坐标    </span><br><span class="line">  _In_      int y,                // 初始 y 坐标    </span><br><span class="line">  _In_      int nWidth,           // 初始 x 方向尺寸    </span><br><span class="line">  _In_      int nHeight,          // 初始 y 方向尺寸</span><br><span class="line">  _In_opt_  HWND hWndParent,      // 父窗口句柄</span><br><span class="line">  _In_opt_  HMENU hMenu,          // 窗口菜单句柄</span><br><span class="line">  _In_opt_  HINSTANCE hInstance,  // 程序实例句柄</span><br><span class="line">  _In_opt_  LPVOID lpParam        // 创建参数</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>ShowWindow()</strong><br>该函数设置指定窗口的显示状态。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">BOOL ShowWindow(</span><br><span class="line">    HWND hWnd,//窗口句柄</span><br><span class="line">    int nCmdShow//指定窗口如何显示</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><strong>GetMessage()</strong> </p>
<p>GetMessage是从调用线程的消息队列里取得一个消息并将其放于指定的结构。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GetMessage(</span><br><span class="line">    LPMSG lpMsg，//指向MSG结构的指针，该结构从线程的消息队列里接收消息信息。</span><br><span class="line">    HWND hWnd，//取得其消息的窗口的句柄。</span><br><span class="line">    UINT wMsgFilterMin，</span><br><span class="line">    UINT wMsgFilterMax</span><br><span class="line">)</span><br><span class="line">lpMsg：指向MSG结构的指针，该结构从线程的消息队列里接收消息信息。</span><br><span class="line">hWnd：取得其消息的窗口的句柄。当其值取NULL时，GetMessage为任何属于调用线程的窗口检索消息，线程消息通过PostThreadMessage寄送给调用线程。</span><br><span class="line">wMsgFilterMin：指定被检索的最小消息值的整数。</span><br><span class="line">wMsgFilterMax：指定被检索的最大消息值的整数。</span><br><span class="line">返回值：如果函数取得WM_QUIT之外的其他消息，返回非零值。如果函数取得WM_QUIT消息，返回值是零。如果出现了错误，返回值是-1。例如，当hWnd是无效的窗口句柄或lpMsg是无效的指针时。若想获得更多的错误信息，请调用GetLastError函数</span><br></pre></td></tr></table></figure>

<p><strong>MSG</strong></p>
<p>用来储存消息的结构体。MSG是Windows程序中的结构体。在Windows程序中，消息是由MSG结构体来表示的。成员变量含义：第一个成员变量hwnd表示消息所属的窗口；第二个成员变量message指定了消息的标识符。第三、第四个成员变量wParam和lParam，用于指定消息的附加信息。最后两个变量分别表示消息投递到消息队列中的时间和鼠标的当前位置。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">typedef struct tagMSG &#123;</span><br><span class="line">    HWND hwnd;//消息所属的窗口</span><br><span class="line">    UINT message;//消息的标识符</span><br><span class="line">    WPARAM wParam;//指定消息的附加信息</span><br><span class="line">    LPARAM lParam;//指定消息的附加信息</span><br><span class="line">    DWORD time;//投递到消息队列中的时间</span><br><span class="line">    POINT pt;//当前鼠标位置</span><br><span class="line">&#125; MSG;</span><br></pre></td></tr></table></figure>

<p><strong>DispatchMessage()</strong></p>
<p>函数功能：该函数分发一个消息给窗口程序。通常消息从GetMessage函数获得或者TranslateMessage函数传递的。消息被分发到回调函数（过程函数)，作用是消息传递给操作系统，然后操作系统去调用我们的回调函数，也就是说我们在窗体的过程函数中处理消息。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">LONG DispatchMessage（</span><br><span class="line">    CONST MSG*lpmsg//指向含有消息的MSG结构的指针。</span><br><span class="line">）;</span><br></pre></td></tr></table></figure>

<p><strong>DefWindowProc()</strong></p>
<p>DefWindowProc函数调用缺省的窗口过程来为应用程序没有处理的任何窗口消息提供缺省的处理。该函数确保每一个消息得到处理。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">LRESULT DefWindowProc(</span><br><span class="line">    HWND hWnd，//指向接收消息的窗口过程的句柄。</span><br><span class="line">    UINT Msg，//指定消息类型。</span><br><span class="line">    WPARAM wParam，//指定其余的、消息特定的信息。该参数的内容与Msg参数值有关。</span><br><span class="line">    LPARAM IParam  //指定其余的、消息特定的信息。该参数的内容与Msg参数值有关。</span><br><span class="line">)；</span><br></pre></td></tr></table></figure>

<p>概念图</p>
<p><img src="https://s2.loli.net/2022/02/11/NxCunHy1BZ7EG6v.png" alt="11.png"></p>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line">// 12345.cpp : Defines the entry point for the application.</span><br><span class="line">//</span><br><span class="line"></span><br><span class="line">#include &quot;stdafx.h&quot;</span><br><span class="line"></span><br><span class="line">LRESULT CALLBACK WindowProc(</span><br><span class="line">							HWND hwnd,							</span><br><span class="line">							UINT uMsg,</span><br><span class="line">							WPARAM wParam,</span><br><span class="line">							LPARAM lParam</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">int APIENTRY WinMain(HINSTANCE hInstance,</span><br><span class="line">                     HINSTANCE hPrevInstance,</span><br><span class="line">                     LPSTR     lpCmdLine,</span><br><span class="line">                     int       nCmdShow)</span><br><span class="line">&#123;</span><br><span class="line"> 	// TODO: Place code here.</span><br><span class="line">	char sz0utBuff[0x80];</span><br><span class="line">	</span><br><span class="line">	//1.定义你的窗口是什么样</span><br><span class="line">	TCHAR ClassName[]=TEXT(&quot;MY FIRST WINDOW&quot;);</span><br><span class="line">	WNDCLASS wndclass = &#123;0&#125;;</span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;</span><br><span class="line">	wndclass.lpszClassName = ClassName;</span><br><span class="line">	wndclass.hInstance = hInstance;</span><br><span class="line">	wndclass.lpfnWndProc = WindowProc;</span><br><span class="line">	RegisterClass(&amp;wndclass);//注册窗口类</span><br><span class="line">	//2.创建并显示窗口</span><br><span class="line">	HWND hwnd = CreateWindow(</span><br><span class="line">				ClassName,</span><br><span class="line">				TEXT(&quot;黑瞳的窗口&quot;),</span><br><span class="line">				WS_OVERLAPPEDWINDOW,</span><br><span class="line">				10,</span><br><span class="line">				10,</span><br><span class="line">				600,</span><br><span class="line">				300,</span><br><span class="line">				NULL,</span><br><span class="line">				NULL,</span><br><span class="line">				hInstance,</span><br><span class="line">				NULL</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">	if(hwnd == NULL)</span><br><span class="line">	&#123;</span><br><span class="line">		sprintf(sz0utBuff,&quot;Error:%d&quot;,GetLastError());</span><br><span class="line">		OutputDebugString(sz0utBuff);</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	ShowWindow(hwnd,SW_SHOW);</span><br><span class="line">	//3.接收消息并处理</span><br><span class="line">	MSG msg;</span><br><span class="line">	BOOL bRet;</span><br><span class="line">	while((bRet=GetMessage(&amp;msg,NULL,0,0))!=0)//取出所有消息</span><br><span class="line">	&#123;</span><br><span class="line">		if(bRet=-1)</span><br><span class="line">		&#123;</span><br><span class="line">			//当前程序有问题</span><br><span class="line">			sprintf(sz0utBuff,&quot;Error:%d&quot;,GetLastError());</span><br><span class="line">			OutputDebugString(sz0utBuff);</span><br><span class="line">		&#125;</span><br><span class="line">		else</span><br><span class="line">		&#123;</span><br><span class="line">			//转换消息</span><br><span class="line">			TranslateMessage(&amp;msg);</span><br><span class="line">			//分发消息</span><br><span class="line">			DispatchMessage(&amp;msg);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">LRESULT CALLBACK WindowProc(</span><br><span class="line">							HWND hwnd,							</span><br><span class="line">							UINT uMsg,</span><br><span class="line">							WPARAM wParam,</span><br><span class="line">							LPARAM lParam</span><br><span class="line">)</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">	return DefWindowProc(hwnd,uMsg,wParam,lParam);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">heit0ng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://heit0ng.gitee.io/2022/02/11/WIN32/">https://heit0ng.gitee.io/2022/02/11/WIN32/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://heit0ng.gitee.io" target="_blank">heit0ng</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2022/02/11/zaOwVjM52ZRoAh3.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/19/python/"><img class="prev-cover" src="https://s2.loli.net/2022/02/19/Q9FRIvSuonhgz4E.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">python基本语法</div></div></a></div><div class="next-post pull-right"><a href="/2022/01/28/%E6%B1%87%E7%BC%96%E7%AC%94%E8%AE%B0/"><img class="next-cover" src="https://s2.loli.net/2022/01/28/Swu3a6UXJ7ZpvLf.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">入门级别的汇编</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">heit0ng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81"><span class="toc-number">1.</span> <span class="toc-text">字符编码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%A3%E5%B8%B8ascii%E8%A1%A8"><span class="toc-number">1.1.</span> <span class="toc-text">正常ascii表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8Eascii%E7%9A%84%E6%89%A9%E5%B1%95%E8%A1%A8"><span class="toc-number">1.2.</span> <span class="toc-text">基于ascii的扩展表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#unicode%E7%BC%96%E7%A0%81"><span class="toc-number">1.3.</span> <span class="toc-text">unicode编码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%AD%98%E5%82%A8unicode"><span class="toc-number">1.4.</span> <span class="toc-text">如何存储unicode</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#BOM%EF%BC%88byte-order-mark%EF%BC%89"><span class="toc-number">1.5.</span> <span class="toc-text">BOM（byte order mark）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#win32%E7%9A%84%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">win32的变量类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#win32%E5%AE%BD%E5%AD%97%E7%AC%A6"><span class="toc-number">3.</span> <span class="toc-text">win32宽字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B"><span class="toc-number">4.</span> <span class="toc-text">进程的创建过程</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E8%BF%9B%E7%A8%8B"><span class="toc-number">4.0.1.</span> <span class="toc-text">1.什么是进程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E8%BF%9B%E7%A8%8B%E5%86%85%E5%AD%98%E7%A9%BA%E9%97%B4%E7%9A%84%E5%9C%B0%E5%9D%80%E5%88%92%E5%88%86"><span class="toc-number">4.0.2.</span> <span class="toc-text">2.进程内存空间的地址划分</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E8%BF%9B%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-number">4.0.3.</span> <span class="toc-text">3.进程的创建</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%BF%9B%E7%A8%8B"><span class="toc-number">5.</span> <span class="toc-text">创建进程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%A5%E6%9F%84%E8%A1%A8"><span class="toc-number">6.</span> <span class="toc-text">句柄表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9B%E7%A8%8B%E7%9B%B8%E5%85%B3API"><span class="toc-number">7.</span> <span class="toc-text">进程相关API</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="toc-number">8.</span> <span class="toc-text">创建线程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="toc-number">9.</span> <span class="toc-text">线程控制</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E8%AE%A9%E7%BA%BF%E7%A8%8B%E5%81%9C%E4%B8%8B%E6%9D%A5"><span class="toc-number">9.1.</span> <span class="toc-text">1.让线程停下来</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%AD%89%E5%BE%85%E7%BA%BF%E7%A8%8B%E7%BB%93%E6%9D%9F"><span class="toc-number">9.2.</span> <span class="toc-text">2.等待线程结束</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E8%AE%BE%E7%BD%AE%EF%BC%8C%E8%8E%B7%E5%8F%96%E7%BA%BF%E7%A8%8B%E4%B8%8A%E4%B8%8B%E6%96%87"><span class="toc-number">9.3.</span> <span class="toc-text">3.设置，获取线程上下文</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%B4%E7%95%8C%E5%8C%BA"><span class="toc-number">10.</span> <span class="toc-text">临界区</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%92%E6%96%A5%E4%BD%93"><span class="toc-number">11.</span> <span class="toc-text">互斥体</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6"><span class="toc-number">12.</span> <span class="toc-text">事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AA%97%E5%8F%A3%E7%9A%84%E6%9C%AC%E8%B4%A8"><span class="toc-number">13.</span> <span class="toc-text">窗口的本质</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E4%B8%AAWindows%E7%A8%8B%E5%BA%8F"><span class="toc-number">14.</span> <span class="toc-text">第一个Windows程序</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(2)/" title="j2ee(2)"><img src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(2)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(2)/" title="j2ee(2)">j2ee(2)</a><time datetime="2023-03-22T15:50:01.860Z" title="发表于 2023-03-22 23:50:01">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(1)/" title="j2ee(1)"><img src="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(1)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(1)/" title="j2ee(1)">j2ee(1)</a><time datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统"><img src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言图书管理系统"/></a><div class="content"><a class="title" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统">C语言图书管理系统</a><time datetime="2022-05-16T14:28:31.730Z" title="发表于 2022-05-16 22:28:31">2022-05-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/07/C++qt/" title="C++Qt"><img src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C++Qt"/></a><div class="content"><a class="title" href="/2022/04/07/C++qt/" title="C++Qt">C++Qt</a><time datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)"><img src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="c++的知识(1)"/></a><div class="content"><a class="title" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)">c++的知识(1)</a><time datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By heit0ng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>